﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UtilsLib.UtilsDataAccess;
using UtilsLib.UtilsType;

namespace UtilsLib.UtilsLog
{
    public static class LogToDB
    {
        private static string _DBConnectionString;
        private static string _DBStoredProcName;
        public static string AppId { get; private set; }
        private static bool _isLogging = false;

        public static int MyProperty { get; set; }

        public static void Initialise(string dbConnectionString, string dbStoredProcName, string _AppId)
        {
            _DBConnectionString = dbConnectionString;
            _DBStoredProcName = dbStoredProcName;
            AppId = _AppId;
            _isLogging = true;
        }

        public static void WriteEntry(List<string> Params)
        {
            if (_isLogging)
            {
                DBConnection dbconnec = new DBConnection(_DBConnectionString);
                StringBuilder strb = new StringBuilder();
                strb.Clear();
                strb.Append(_DBStoredProcName);

                bool first = true;
                foreach (string str in Params)
                {
                    if (first)
                    {
                        first = false;
                        strb.Append(" ");
                    }
                    else
                        strb.Append(", ");
                    strb.Append(str);
                }

                try
                {
                    dbconnec.StartConnection();
                    dbconnec.ExecNonQuery(strb);
                }
                catch (Exception ex)
                {
                    LogToFile.WriteException(ex);
                }
                finally
                {
                    dbconnec.EndConnection();
                }
            }
        }

        private static List<string> GetParamList(string DateApparition, TypeMessage Type, string Domain, string URI, string QueryString, string URIRewrite, string Controller, string Action, string Description, string RemoteAddr, string RemoteHost, string UserAgent)
        {
            List<string> result = new List<string>();
            result.Add(AppId);
            result.Add(DateApparition);
            result.Add(Type.ToString());
            result.Add(Domain);
            result.Add(URI);
            result.Add(QueryString);
            result.Add(URIRewrite);
            result.Add(Controller);
            result.Add(Action);
            result.Add(Description);
            result.Add(RemoteAddr);
            result.Add(RemoteHost);
            result.Add(UserAgent);

            return result;
        }


        /// <summary>
        /// Write a line in the log
        /// </summary>
        /// <param name="message">The message to write in the log</param>
        public static void WriteMessage(string Description, string Domain = "", string URI = "", string QueryString = "", string URIRewrite = "", string Controller = "", string Action = "", string RemoteAddr = "", string RemoteHost = "", string UserAgent = "")
        {
            List<string> spParam = GetParamList(DateTime.Now.ToString(), TypeMessage.Message, Domain, URI, QueryString, URIRewrite, Controller, Action, Description, RemoteAddr, RemoteHost, UserAgent);

            WriteEntry(spParam);
        }



        /// <summary>
        /// Write an exception line in the log with various informations
        /// </summary>
        /// <param name="ex">The exception</param>
        public static void WriteException(Exception ex, string Domain = "", string URI = "", string QueryString = "", string URIRewrite = "", string Controller = "", string Action = "", string RemoteAddr = "", string RemoteHost = "", string UserAgent = "")
        {
            List<string> spParam = GetParamList(DateTime.Now.ToString(), TypeMessage.Error, Domain, URI, QueryString, URIRewrite, Controller, Action, ex.Message + " \r\n|\r\n " + ex.StackTrace, RemoteAddr, RemoteHost, UserAgent);

            WriteEntry(spParam);
        }


        /// <summary>
        /// Write an Error line in the log when something is not supposed to happen but does !
        /// </summary>
        /// <param name="message">The message(s) to describe the error</param>
        public static void WriteError(string Description, string Domain = "", string URI = "", string QueryString = "", string URIRewrite = "", string Controller = "", string Action = "", string RemoteAddr = "", string RemoteHost = "", string UserAgent = "")
        {
            List<string> spParam = GetParamList(DateTime.Now.ToString(), TypeMessage.Error, Domain, URI, QueryString, URIRewrite, Controller, Action, Description, RemoteAddr, RemoteHost, UserAgent);

            WriteEntry(spParam);
        }

        /// <summary>
        /// Write a warning in the log
        /// </summary>
        /// <param name="message">The message to describe the warning</param>
        public static void WriteWarning(string Description, string Domain = "", string URI = "", string QueryString = "", string URIRewrite = "", string Controller = "", string Action = "", string RemoteAddr = "", string RemoteHost = "", string UserAgent = "")
        {
            List<string> spParam = GetParamList(DateTime.Now.ToString(), TypeMessage.Warning, Domain, URI, QueryString, URIRewrite, Controller, Action, Description, RemoteAddr, RemoteHost, UserAgent);

            WriteEntry(spParam);
        }



    }
}
